<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@ include file="/common.jsp" %>
<script type="text/javascript">
var dataGrid;
$(function() {
    dataGrid = $('#dataGrid').datagrid({
        url : '${pageContext.request.contextPath}/task/taskJob/dataGrid',
        fit : true,
        striped : true,
        rownumbers : false,
        pagination : true,
        singleSelect : true,
        idField : 'id',
        sortName : 'id',
        sortOrder : 'asc',
        pageSize : 20,
        pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
        frozenColumns : [[{
	            width : '150',
	            title : 'ID',
	            field : 'id',
	            sortable : true
	        }
        ]],
        columns : [ [ 
   		{
            width : '100',
            title : '任务名称',
            field : 'jobName',
            sortable : true
       },  
   		{
            width : '100',
            title : '任务分组',
            field : 'jobGroup',
            sortable : true
       },  
   		{
            width : '100',
            title : '任务启动状态',
            field : 'jobStatus',
            sortable : true,
            formatter : function(value, row, index) {
            	if(value==0) return '未启动';
            	else if(value==1) return '已启动';
            }
       },  
   		{
            width : '100',
            title : 'cron表达式',
            field : 'cron',
            sortable : true
       },  
   		{
            width : '100',
            title : '描述',
            field : 'description',
            sortable : true
       },  
   		{
            width : '100',
            title : 'springBeanID',
            field : 'beanId',
            sortable : true
       },  
   		{
            width : '100',
            title : 'beanClass',
            field : 'beanClass',
            sortable : true
       },  
   		{
            width : '100',
            title : '任务调用的方法名',
            field : 'methodName',
            sortable : true
       },  
   		{
            width : '100',
            title : '并发状态',
            field : 'isConcurrent',
            sortable : true,
            formatter : function(value, row, index) {
            	if(value==0) return '非并发任务';
            	else if(value==1) return '并发任务';
            }
       },  
   		{
            width : '100',
            title : '更新时间',
            field : 'updateTime',
            sortable : true
       },  
		{
            width : '130',
            title : '创建时间',
            field : 'createTime',
            sortable : true
        }/**,   
        {
            field : 'action',
            title : '操作',
            width : 250,
            formatter : function(value, row, index) {
                var str = '';
			        if(row.jobStatus == 0){
			        	<shiro:hasPermission name="task/taskJob/resume">
			        		str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
			        		str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-resume" data-options="plain:true,iconCls:\'fi-play icon-black\'" onclick="resumeFun(\'{0}\');" >启动任务</a>', row.id);
						</shiro:hasPermission>
			        }
			        if(row.jobStatus == 1){
			        	<shiro:hasPermission name="task/taskJob/pause">
			        		str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
			        		str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-pause" data-options="plain:true,iconCls:\'fi-pause icon-black\'" onclick="pauseFun(\'{0}\');" >暂停任务</a>', row.id);
						</shiro:hasPermission>
			        }
                if(str==''){
                	str = '您尚无[操作权限]';
                }    
                return str;
            }
        }*/] ],
        onLoadSuccess:function(data){
            $('.user-easyui-linkbutton-edit').linkbutton({text:'编辑',plain:true,iconCls:'fi-pencil icon-black'});
            $('.user-easyui-linkbutton-del').linkbutton({text:'删除',plain:true,iconCls:'fi-x icon-black'});
            
            $('.user-easyui-linkbutton-resume').linkbutton({text:'启动任务',plain:true,iconCls:'fi-play icon-black'});
            $('.user-easyui-linkbutton-pause').linkbutton({text:'暂停任务',plain:true,iconCls:'fi-pause icon-black'});
        },
        toolbar : '#toolbar'
    });
});

function addFun() {
    parent.$.modalDialog({
        title : '添加',
        width : 600,
        height : 500,
        href : '${pageContext.request.contextPath}/task/taskJob/addPage',
        buttons : [ {
        		text : '取消添加',
            	iconCls:'fi-x icon-black',
            	handler:function(){
            		parent.$.modalDialog.handler.dialog('destroy');
            		parent.$.modalDialog.handler = undefined;
            	}
            },{
            text : '确定添加',
            iconCls:'fi-check icon-black',
            handler : function() {
                parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                var f = parent.$.modalDialog.handler.find('#addForm');
                f.submit();
            }
        } ]
    });
}

function deleteFun(id) {
    try{
		if (id == undefined) {
	        var rows = dataGrid.datagrid('getSelections');
	        id = rows[0].id;
	    } else {
	        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	    }
	}catch(e){
		if(id == undefined){
	    	parent.$.messager.alert('提示', '请选择一个想要删除的任务！', 'info');
	    	return ;
	    }
	}
    parent.$.messager.confirm('询问', '您是否要删除当前数据吗？', function(b) {
        if (b) {
            progressLoad();
            $.post('${pageContext.request.contextPath}/task/taskJob/delete', {
                id : id
            }, function(result) {
                if (result.success) {
                    parent.$.messager.alert('提示', result.msg, 'info');
                    dataGrid.datagrid('reload');
                } else {
                    parent.$.messager.alert('错误', result.msg, 'error');
                }
                progressClose();
            }, 'JSON');
        }
    });
}

function editFun(id) {
    try{
		if (id == undefined) {
	        var rows = dataGrid.datagrid('getSelections');
	        id = rows[0].id;
	    } else {
	        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	    }
	}catch(e){
		if(id == undefined){
	    	parent.$.messager.alert('提示', '请选择一个想要编辑的任务！', 'info');
	    	return ;
	    }
	}
    parent.$.modalDialog({
        title : '编辑',
        width : 500,
        height : 600,
        href : '${pageContext.request.contextPath}/task/taskJob/editPage?id=' + id,
        buttons : [ {
        		text : '取消修改',
            	iconCls:'fi-x icon-black',
            	handler:function(){
            		parent.$.modalDialog.handler.dialog('destroy');
            		parent.$.modalDialog.handler = undefined;
            	}
            },{
            text : '确定修改',
            iconCls:'fi-check icon-black',
            handler : function() {
                parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                var f = parent.$.modalDialog.handler.find('#editForm');
                f.submit();
            }
        } ]
    });
}
//立即执行
function runFun(id){
	try{
		if (id == undefined) {
	        var rows = dataGrid.datagrid('getSelections');
	        id = rows[0].id;
	    } else {
	        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	    }
	}catch(e){
		if(id == undefined){
	    	parent.$.messager.alert('提示', '请选择一个想要执行的任务！', 'info');
	    	return ;
	    }
	}
    
    parent.$.messager.confirm('询问', '您是否要立即执行当前任务吗？', function(b) {
        if (b) {
            progressLoad();
            $.post('${pageContext.request.contextPath}/task/taskJob/run', {
                id : id
            }, function(result) {
                if (result.success) {
                    parent.$.messager.alert('提示', result.msg, 'info');
                    dataGrid.datagrid('reload');
                } else {
                    parent.$.messager.alert('错误', result.msg, 'error');
                }
                progressClose();
            }, 'JSON');
        }
    });
}
//启动任务
function resumeFun(id){
	var jobStatus = -1;
	try{
		if (id == undefined) {
	        var rows = dataGrid.datagrid('getSelections');
	        id = rows[0].id;
	        jobStatus = rows[0].jobStatus;
	    } else {
	        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	    }
	}catch(e){
		if(id == undefined){
	    	parent.$.messager.alert('提示', '请选择一个想要启动的任务！', 'info');
	    	return ;
	    }
	}
	if(jobStatus == 1){
		parent.$.messager.alert('提示', '该任务已是启动状态，无需再次启动！', 'info');
	 	return ;
	}
    parent.$.messager.confirm('询问', '您是否要启动当前任务吗？', function(b) {
        if (b) {
            progressLoad();
            $.post('${pageContext.request.contextPath}/task/taskJob/resume', {
                id : id
            }, function(result) {
                if (result.success) {
                    parent.$.messager.alert('提示', result.msg, 'info');
                    dataGrid.datagrid('reload');
                } else {
                    parent.$.messager.alert('错误', result.msg, 'error');
                }
                progressClose();
            }, 'JSON');
        }
    });
}
//暂停任务
function pauseFun(id){
	var jobStatus = -1;
	try{
		if (id == undefined) {
	        var rows = dataGrid.datagrid('getSelections');
	        id = rows[0].id;
	        jobStatus = rows[0].jobStatus;
	    } else {
	        dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
	    }
	}catch(e){
		if(id == undefined){
	    	parent.$.messager.alert('提示', '请选择一个想要暂停的任务！', 'info');
	    	return ;
	    }
	}
	if(jobStatus == 0){
		parent.$.messager.alert('提示', '该任务已是暂停状态，无需再次暂停！', 'info');
	 	return ;
	}
    parent.$.messager.confirm('询问', '您是否要暂停当前任务吗？', function(b) {
        if (b) {
            progressLoad();
            $.post('${pageContext.request.contextPath}/task/taskJob/pause', {
                id : id
            }, function(result) {
                if (result.success) {
                    parent.$.messager.alert('提示', result.msg, 'info');
                    dataGrid.datagrid('reload');
                } else {
                    parent.$.messager.alert('错误', result.msg, 'error');
                }
                progressClose();
            }, 'JSON');
        }
    });
}

function searchFun() {
    dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
}
function cleanFun() {
    $('#searchForm input').val('');
    dataGrid.datagrid('load', {});
}
</script>
<body class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'north',border:false" style="height: 30px; overflow: hidden;background-color: #fff">
        <form id="searchForm">
            <table>
                <tr>
                    <th>创建时间:</th>
                    <td>
                    	<input name="createTimeStart" placeholder="点击选择时间" 
                    		onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />
                                                                        至<input  name="createTimeEnd" placeholder="点击选择时间" 
                            onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />
                   </td>
                   <td>         
                    	<a href="javascript:void(0);" class="easyui-linkbutton" 
                    		data-options="iconCls:'fi-magnifying-glass icon-black',plain:true" onclick="searchFun();">查询</a>
                    	<a href="javascript:void(0);" class="easyui-linkbutton" 
                    		data-options="iconCls:'fi-x icon-black',plain:true" onclick="cleanFun();">清空</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',border:true,title:'列表数据'" >
        <table id="dataGrid" data-options="fit:true,border:false"></table>
    </div>
    <div id="toolbar" style="display: none;">
        <shiro:hasPermission name="task/taskJob/add">
            <a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-plus icon-black'">添加</a>
        </shiro:hasPermission>
        <shiro:hasPermission  name="task/taskJob/edit">
        	<a onclick="editFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-pencil icon-black'">编辑</a>
        </shiro:hasPermission>  
        <shiro:hasPermission name="task/taskJob/delete">
        	<a onclick="deleteFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-x icon-black'">删除</a>
        </shiro:hasPermission>
        
        <shiro:hasPermission name="task/taskJob/run">
        	<a onclick="runFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-play-circle icon-black'">立即执行</a>
		</shiro:hasPermission>   
       	<shiro:hasPermission name="task/taskJob/resume">
       		<a onclick="resumeFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-play icon-black'">启动任务</a>
		</shiro:hasPermission>
       	<shiro:hasPermission name="task/taskJob/pause">
       		<a onclick="pauseFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-pause icon-black'">暂停任务</a>
		</shiro:hasPermission>
    </div>
</body>
</html>
